//
//  PPWaterFallsView.m
//  pet
//
//  Created by 陈无忌 on 12/12/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

#import "PPWaterFallsView.h"

@implementation PPWaterFallsView



- (void)setWithDataArray:(NSArray*)array
{
    self.imageArray = array;
    //初始化参数
    [self initParameter];
}

-(void)initParameter
{
    for (UIView *subviews in [self subviews]) {
        [subviews removeFromSuperview];
    }
    //每一列的视图初始化
    firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 0)];
    secondView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH, 0)];
    thridView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH*2, 0, WIDTH, 0)];
    
    higher = row = highValue = lower = 1;
    countImage = 0;
    
    for (int i = 0; i<self.imageArray.count; i++) {
        //0%3=0,0-2除3也不可能大于0
        if (i/3>0 && i%3==0) {
            row++;
        }
        id object = [self.imageArray objectAtIndex:i];
        countImage ++;
        //添加视图
//        if ([object isKindOfClass:[petPhotoModel class]]) {
            petPhotoModel *model = (petPhotoModel *)object;
            [self addViewsWithModel:model with:countImage];
//        }else if ([object isKindOfClass:[UIImage class]]){
//            UIImage *image = (UIImage *)object;
//            [self addViewsWithImage:image with:countImage];
//        }
        //重新设置最高和最低view
        [self setHigherAndLower];
    }

    [self setContentSize:CGSizeMake(WIDTH, highValue)];
    [self addSubview:firstView];
    [self addSubview:secondView];
    [self addSubview:thridView];
}

//-(void)addViewsWithImage:(UIImage *)image with:(int)a
//{
//    //要添加到列上的图片对象
//    PPWateFallDetailView *imageView = [[PPWateFallDetailView alloc] init];
//    //图片的高度
//    float imageHeight = 0;
//    //创建每列的视图填充的内容
//    /*
//     1、创建自定义的图片对象
//     2、记住该图片的高度
//     3、重新定义每列的大小，就是高度
//     4、把该图片加到每列上。
//     */
//    //在最低的那一列添加图片
//    switch (lower) {
//        case 1:
//            [imageView drawViewWithImg:image AndY:firstView.frame.size.height];
//            imageHeight = imageView.frame.size.height;
//            firstView.frame = CGRectMake(firstView.frame.origin.x, firstView.frame.origin.y, WIDTH, firstView.frame.size.height + imageHeight);
//            [firstView addSubview:imageView];
//            break;
//        case 2:
//            [imageView drawViewWithImg:image AndY:secondView.frame.size.height];
//            imageHeight = imageView.frame.size.height;
//            secondView.frame = CGRectMake(secondView.frame.origin.x, secondView.frame.origin.y, WIDTH, secondView.frame.size.height + imageHeight);
//            [secondView addSubview:imageView];
//            break;
//        case 3:
//            [imageView drawViewWithImg:image AndY:thridView.frame.size.height];
//            imageHeight = imageView.frame.size.height;
//            thridView.frame = CGRectMake(thridView.frame.origin.x, thridView.frame.origin.y, WIDTH, thridView.frame.size.height + imageHeight);
//            [thridView addSubview:imageView];
//            break;
//        default:
//            break;
//    }
//    imageView.delegate = self;
//}



-(void)addViewsWithModel:(petPhotoModel *)model with:(int)a
{
    //要添加到列上的图片对象
    PPWateFallDetailView *imageView = [[PPWateFallDetailView alloc] init];
    //图片的高度
    float imageHeight = 0;
    //创建每列的视图填充的内容
    /*
     1、创建自定义的图片对象
     2、记住该图片的高度
     3、重新定义每列的大小，就是高度
     4、把该图片加到每列上。
     */
    //在最低的那一列添加图片
    switch (lower) {
        case 1:
            [imageView drawViewWithImg:model AndY:firstView.frame.size.height];
            imageHeight = imageView.frame.size.height;
            firstView.frame = CGRectMake(firstView.frame.origin.x, firstView.frame.origin.y, WIDTH, firstView.frame.size.height + imageHeight);
            [firstView addSubview:imageView];
            break;
        case 2:
            [imageView drawViewWithImg:model AndY:secondView.frame.size.height];
            imageHeight = imageView.frame.size.height;
            secondView.frame = CGRectMake(secondView.frame.origin.x, secondView.frame.origin.y, WIDTH, secondView.frame.size.height + imageHeight);
            [secondView addSubview:imageView];
            break;
        case 3:
            [imageView drawViewWithImg:model AndY:thridView.frame.size.height];
            imageHeight = imageView.frame.size.height;
            thridView.frame = CGRectMake(thridView.frame.origin.x, thridView.frame.origin.y, WIDTH, thridView.frame.size.height + imageHeight);
            [thridView addSubview:imageView];
            break;
        default:
            break;
    }
    imageView.delegate = self;
}

-(void)setHigherAndLower
{
    float firstHeight = firstView.frame.size.height;
    float secondHeight = secondView.frame.size.height;
    float thridHeight = thridView.frame.size.height;
    //比较哪一列是最高的那列，并记录最高的值highValue和最高的列higher
    if (firstHeight > highValue) {
        highValue = firstHeight;
        higher = 1;
    }else if (secondHeight > highValue){
        highValue = secondHeight;
        higher = 2;
    }else if (thridHeight > highValue){
        highValue = thridHeight;
        higher = 3;
    }
    //找了最低列
    if (firstHeight < secondHeight) {
        if (firstHeight <= thridHeight) {
            lower = 1;
        }else{
            lower = 3;
        }
    }else{
        if (secondHeight <= thridHeight) {
            lower = 2;
        }else{
            lower = 3;
        }
    }
}

-(void)clickImage:(UIImage *)data
{
    //判断是否处于编辑模式
    if (self.isEdit) {
        //自己做处理
        NSLog(@"点击了图片：%@",data);
        NSDictionary *userInfo = @{@"img":data};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"reciveClickPhoto" object:nil userInfo:userInfo];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
